﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using InTheHand.Storage;
using Windows.UI.Popups;
using System.Runtime.InteropServices;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace WindowsPhone81Test
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            System.Diagnostics.Debug.WriteLine(Windows.ApplicationModel.Package.Current);
            InTheHand.ApplicationModel.Search.SearchPane.GetForCurrentView().QuerySubmitted += MainPage_QuerySubmitted;
            DevMode.Text = InTheHand.ApplicationModel.Package.Current.IsDevelopmentMode.ToString();
            this.NavigationCacheMode = NavigationCacheMode.Required;

            /*IntPtr ptr;
            int err = JetCreateInstance2(out ptr, "Test", "TestESEInstance", 0);
            err = JetInit3(ref ptr, IntPtr.Zero, 0);
            IntPtr sess;
            err = JetBeginSession(ptr, out sess, IntPtr.Zero, IntPtr.Zero);

            IntPtr dbid;
            err = JetCreateDatabase2(sess, "mydb.edb", 0, out dbid, 0);

            if(ptr != IntPtr.Zero)
            {
                err = JetTerm2(ptr, 0);
            }*/
        }

        [DllImport("ESENT.dll")]
        private static extern int JetCreateInstance2(out IntPtr pinstance, string szInstanceName, string szDisplayName, int grbit);

        [DllImport("ESENT.dll")]
        private static extern int JetInit3(ref IntPtr pinstance, IntPtr prstInfo, int grbit);

        [DllImport("ESENT.dll")]
        private static extern int JetBeginSession(IntPtr instance, out IntPtr psesid, IntPtr szUserName, IntPtr szPassword);


        [DllImport("ESENT.dll")]
        private static extern int JetCreateDatabase2(IntPtr sesid, string szFilename, ulong cpgDatabaseSizeMax, out IntPtr pdbid, int grbit);

         [DllImport("ESENT.dll")]
        private static extern int JetTerm2(IntPtr instance, int grbit);

        async void MainPage_QuerySubmitted(InTheHand.ApplicationModel.Search.SearchPane sender, InTheHand.ApplicationModel.Search.SearchPaneQuerySubmittedEventArgs args)
        {
            MessageDialog md = new MessageDialog("searched for " + args.QueryText);
            await md.ShowAsync();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.
            await ApplicationData.Current.LocalFolder.CreateFileAsync("one.txt", CreationCollisionOption.OpenIfExists);
            await ApplicationData.Current.LocalFolder.CreateFileAsync("two.txt", CreationCollisionOption.OpenIfExists);
            await ApplicationData.Current.LocalFolder.CreateFileAsync("three.txt", CreationCollisionOption.OpenIfExists);
            System.Diagnostics.Debug.WriteLine(Environment.TickCount);
            IStorageItem item4 = await ApplicationData.Current.LocalFolder.TryGetItemAsync("four.txt");
            System.Diagnostics.Debug.WriteLine(Environment.TickCount);
            System.Diagnostics.Debug.WriteLine(item4);
            IStorageItem item2 = await ApplicationData.Current.LocalFolder.TryGetItemAsync("two.txt");
            System.Diagnostics.Debug.WriteLine(Environment.TickCount);
            System.Diagnostics.Debug.WriteLine(item2);
            IStorageItem item5 = await ApplicationData.Current.LocalFolder.TryGetItemAsync("five.txt");
            System.Diagnostics.Debug.WriteLine(Environment.TickCount);
            System.Diagnostics.Debug.WriteLine(item5);
            
            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }

        private void AppBarButton_Click(object sender, RoutedEventArgs e)
        {
            InTheHand.UI.ApplicationSettings.SettingsPane.Show();
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            InTheHand.ApplicationModel.Search.SearchPane.GetForCurrentView().Show();
        }
    }
}
